package com.hellloop.pim.alphabeat.network;

import java.io.IOException;
import java.util.*;

import com.hellloop.pim.alphabeat.entity.LivingRoomEntity;
import com.hellloop.pim.alphabeat.entity.Player;
import com.hellloop.pim.alphabeat.entity.RoomProperties;
import com.hellloop.pim.alphabeat.entity.Sentence;
import com.hellloop.pim.alphabeat.game.PIMClientGame;

public class PIMClient implements PIMClientInterface {

	private List<PIMMessage> recMessageQueue;
	private PIMClientMessageHandler clientMessageHandler;
	private PIMClientConnectionThread clientConnectionThread;
	private PIMClientGame clientGame;

	public PIMClient() {
		// Empty Constructor
		recMessageQueue = new LinkedList<PIMMessage>();
	}

	public PIMClient(PIMClientGame pClientGame) {
		this();
		clientGame = pClientGame;
		recMessageQueue = new LinkedList<PIMMessage>();
	}

	public void openConnection(String host, int port) throws IOException {
		clientConnectionThread = new PIMClientConnectionThread(this, host,
				port, recMessageQueue);
		clientMessageHandler = new PIMClientMessageHandler(this, this.recMessageQueue);
	}

	public void closeConnection() throws IOException {
		clientConnectionThread.closeConnection();
	}

	public void sendMessageToServer(PIMMessage message) {
		clientConnectionThread.sendMessage(message);
	}

	@Override
	public void connect(String hostName, int port, String login, String password)
			throws IOException {
		
		openConnection(hostName, port);
		
		/*PIMMessage loginRequest = new PIMMessage();
		loginRequest.setCommand("loginRequest");
		loginRequest.addOperand(playerName);*/
		PIMConnectionMsg loginRequest = new PIMConnectionMsg();
		loginRequest.setOprType(PIMConnectionMsg.RequestLogin);
		loginRequest.setUserName(login);
		loginRequest.setPassWord(password); // TODO Hardcode
		
		clientConnectionThread.sendMessage(loginRequest);
		
	}
	
	protected void doConnectSuccess(int id) {
		System.out.println("CONNECT SUCCESS!!!");
		System.out.println("id = " + id);
		clientGame.doConnectSuccess(id);
	}

	@Override
	public void createRoom(int userId, RoomProperties roomProp) {
		/*PIMMessage createRoomRequest = new PIMMessage();
		createRoomRequest.setCommand("createRoomRequest");
		createRoomRequest.addOperand(roomName);*/
		PIMRoomMsg createRoomRequest = new PIMRoomMsg();
		createRoomRequest.setOprType(PIMRoomMsg.CreateRoom);
		createRoomRequest.setRoomProp(roomProp);
		createRoomRequest.setUserID(userId);
		
		System.out.println("CreateRoomRequest Sent");
		clientConnectionThread.sendMessage(createRoomRequest);
		
	}
	
	public void doCreateRoomSuccess(int roomId) {
		System.out.println("CREATE ROOM SUCCESS!!!");
		clientGame.doCreateRoomSuccess(roomId);
	}

	public void doReceiveSentenceListSuccess(Sentence[] sentenceArray){
		System.out.println("RECEIVE SENTENCE LIST");
		clientGame.gameStart(sentenceArray);
	}
	
	@Override
	public void exitRoom() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void joinRoom(int playerId, int roomId) {
		// TODO Auto-generated method stub
		PIMRoomMsg joinRoomRequest = new PIMRoomMsg();
		joinRoomRequest.setPlayerId("" + playerId);
		joinRoomRequest.setRoomId("" + roomId);
		clientConnectionThread.sendMessage(joinRoomRequest);
	}
	
	public void doJoinRoomSuccess(RoomProperties roomProp, Player[] playerList){
		clientGame.doJoinRoomSuccess(roomProp, playerList);
	}

	@Override
	public void requestSentences() {
		//PIMMessage sentenceRequest = new PIMMessage();
		//sentenceRequest.setCommand("sentenceRequest");
		
		//clientConnectionThread.sendMessage(sentenceRequest);
		
		PIMGameMsg sentenceRequest = new PIMGameMsg();
		sentenceRequest.setRoomId("100");
		clientConnectionThread.sendMessage(sentenceRequest);
		
	}

	@Override
	public void ready(int playerId, boolean ready) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startGame(int roomId) {
		// TODO Auto-generated method stub
		
	}

}
